<!--
 * @Description: 轮播配置页面
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 15:22:22
-->

<template>
  <div>
    <!-- 按钮 -->
    <div>
      <el-button size="small" type="primary" @click="toAdd">新增</el-button>
      <el-button size="small" type="primary" @click="BigScreenVisible=true">预览</el-button>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="name" label="轮播图名称" align="center" />
        <el-table-column prop="introduce" label="轮播图描述" align="center" />
        <el-table-column label="轮播图图片" align="center">
          <template #default="scope">
            <el-popover
              placement="right"
              trigger="hover"
            >
              <el-image class="img__hoverImg" fit="contain" :src="scope.row.url" />
              <el-image slot="reference" class="img__innerImg" fit="contain" :src="scope.row.url" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.status == '正常'"
              type="success"
            >正常</el-tag>
            <el-tag v-else type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              style="color: #e65d6e"
              @click="toDel(scope.row.id)"
            >删除</el-button>
            <el-button v-show="scope.row.status === '停用'" type="text" style="color: #3a71a8" @click="stopUse(scope.row)">启用</el-button>
            <el-button v-show="scope.row.status === '正常'" type="text" style="color: #3a71a8" @click="stopUse(scope.row)">停用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 模态框 -->
    <div>
      <el-dialog
        :title="title"
        :visible.sync="visible"
        width="50%"
        @close="toClose"
      >
        <!-- {{ form }} -->
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="100px"
          class="demo-form"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="轮播图片" prop="url">
            <el-upload
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="描述" prop="introduce">
            <el-input v-model="form.introduce" type="textarea" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 大屏预览模态框 -->
    <div>
      <el-dialog
        title="大屏预览"
        :visible.sync="BigScreenVisible"
        width="80%"
      >
        <div>
          <el-carousel trigger="click" height="300px">
            <el-carousel-item v-for="item in carouselList" :key="item.id">
              <el-image style="width: 100%; height: 500px" :src="item.url" />
            </el-carousel-item>
          </el-carousel>
        </div>
        <span slot="footer">
          <el-button type="primary" @click="BigScreenVisible = false">关 闭</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { findAll, saveOrUpdate, deleteById, query } from '@/api/carousel'
import { uploadFileURL, showFileURL } from '@/utils/config'

export default {
  data() {
    return {
      // 表格数据
      tableData: {},
      // 模态框标题
      title: '',
      // 模态框显示
      visible: false,
      // 表单数据
      form: {},
      uploadFileURL,
      showFileURL,
      imageUrl: '',
      // 校验规则
      rules: {
        name: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' }
        ],
        url: [{ required: true, message: '请上传轮播图', trigger: 'change' }]
      },
      // 大屏模态框
      BigScreenVisible: false,
      // 轮播图数组
      carouselList: []
    }
  },
  computed: {},
  created() {
    this.queryTableData()
  },
  mounted() {},
  methods: {
    // 获取轮播图数据
    async queryTableData() {
      const res = await findAll()
      // console.log(res);
      this.tableData = res.data
      // console.log(this.tableData)
      this.getCarouselList()
    },
    // 图片上传成功后的回调函数
    handleAvatarSuccess(res, file) {
      // console.log(file);
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.url = showFileURL + res.data.id
    },
    // 新增按钮点击处理事件
    toAdd() {
      this.title = '新增轮播图'
      this.form = {}
      this.imageUrl = ''
      this.visible = true
    },
    // 重置表单验证
    toClose() {
      this.$refs.form.resetFields()
    },
    // 确定按钮点击处理事件
    toSave() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          if (res.status === 200) {
            this.$notify({
              title: '成功',
              message: res.message,
              type: 'success'
            })
            this.queryTableData()
            this.toClose()
            this.visible = false
          }
        } else {
          return false
        }
      })
    },
    // 编辑按钮点击处理事件
    toEdit(row) {
      this.title = '编辑轮播图'
      this.form = { ...row }
      this.imageUrl = row.url
      this.visible = true
    },
    // 删除按钮触发事件
    toDel(id) {
      // console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id: id })
        // console.log(res);
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.queryTableData()
        } else {
          this.$message({
            type: 'danger',
            message: '删除失败!'
          })
        }
      })
    },
    // 停用、启用 按钮触发的事件
    async stopUse(row) {
      console.log(row)
      if (row.status === '正常') row.status = '停用'
      else row.status = '正常'
      await saveOrUpdate({ ...row })
      this.queryTableData()
    },
    // 查询正常状态的轮播图
    async getCarouselList() {
      const res = await query({ status: '正常' })
      this.carouselList = res.data
      // console.log(this.carouselList)
    }
  }
}
</script>
<style scoped>
.img__innerImg {
  width: 30px;
  height: 30px;
}
.img__hoverImg {
  width: 180px;
  height: 120px;
}
</style>
